﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Utility;

public partial class StrategicPlan_StrategicPlanAdd : System.Web.UI.Page
{
    Authentication dllAuthentication = new Authentication();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();

        if (objUserInfo.UserName == "admin")
        {
            this.btnAdd.Enabled = true;
            this.btnSave.Enabled = true;
        }
        else
        {
            this.btnSave.Enabled = false;
            this.btnAdd.Enabled = false;
        }

        if (!IsPostBack)
        {
            bindDropDownList(this.drpStrategicDate);
            bindDropDownList(this.drpStrategyCategory);
            bindDropDownList(this.drpDepartment);
            bindGridView();
        }

        if (this.gdvStrategicPlan.Rows.Count == 0)
        {
            this.txtStrategicPlanSortId.Text = "1";
        }
        else
        {
            this.txtStrategicPlanSortId.Text = Convert.ToString(this.gdvStrategicPlan.Rows.Count + 1);
        }
    }

    /// <summary>
    /// 绑定战略类别下拉
    /// </summary>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpStrategyCategory))
        {
            StrategicCategoryMgr dllStrategicCategoryMgr = new StrategicCategoryMgr();
            this.drpStrategyCategory.DataSource = dllStrategicCategoryMgr.get();
            this.drpStrategyCategory.DataTextField = "StrategicCategoryName";
            this.drpStrategyCategory.DataValueField = "StrategicCategoryId";
            this.drpStrategyCategory.DataBind();
        }
        if (_drp.Equals(this.drpStrategicDate))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(li);
            }
            //根据系统日期自动绑定到当前年份
            string _year = DateTime.Now.Year.ToString();
            this.drpStrategicDate.SelectedIndex = this.drpStrategicDate.Items.IndexOf(this.drpStrategicDate.Items.FindByText(_year));
        }
        if (_drp.Equals(this.drpDepartment))
        {
            DepartmentInfoManager dllDepartmentInfoManager = new DepartmentInfoManager();
            DataTable dt = dllDepartmentInfoManager.getNoTree();
            this.drpDepartment.DataSource = dt;
            this.drpDepartment.DataTextField = "DepartmentName";
            this.drpDepartment.DataValueField = "DepartmentId";
            this.drpDepartment.DataBind();
        }
    }


    /// <summary>
    /// 绑定战略计划
    /// </summary>
    void bindGridView()
    {
        StrategicPlanMgr dllStrategicPlanMgr = new StrategicPlanMgr();
        object[] _value = new object[1];
        _value[0] = this.drpStrategicDate.SelectedItem.Text;
        this.gdvStrategicPlan.DataSource = dllStrategicPlanMgr.get(StrategicPlanMgr.field.StrategicPlanYear.ToString(), _value);
        this.gdvStrategicPlan.DataBind();
    }




    /// <summary>
    /// 添加战略计划事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (this.drpStrategyCategory.Items.Count > 0)
        {
            DataTable dt = genDataTable();
            for (int i = 0; i < this.gdvStrategicPlan.Rows.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["StrategicPlanId"] = this.gdvStrategicPlan.DataKeys[i]["StrategicPlanId"].ToString();
                dr["StrategicPlanSortId"] = this.gdvStrategicPlan.Rows[i].Cells[1].Text;
                dr["StrategicPlanYear"] = this.drpStrategicDate.SelectedItem.Text;
                dr["CreateDate"] = this.gdvStrategicPlan.DataKeys[i]["CreateDate"].ToString();
                dr["WorkItem"] = this.gdvStrategicPlan.Rows[i].Cells[2].Text;
                dr["StartTime"] = this.gdvStrategicPlan.Rows[i].Cells[3].Text;
                dr["CompleteTime"] = this.gdvStrategicPlan.Rows[i].Cells[4].Text;

                dr["MasterDepartment"] = this.gdvStrategicPlan.Rows[i].Cells[5].Text;
                dr["MasterPeople"] = this.gdvStrategicPlan.Rows[i].Cells[6].Text;
                dr["StrategicCategory"] = this.gdvStrategicPlan.Rows[i].Cells[7].Text;

                dr["StrategicPlanId"] = Comm.RemoveSpecialSymbols(dr["StrategicPlanId"].ToString());
                dr["StrategicPlanSortId"] = Comm.RemoveSpecialSymbols(dr["StrategicPlanSortId"].ToString());
                dr["StrategicPlanYear"] = Comm.RemoveSpecialSymbols(dr["StrategicPlanYear"].ToString());
                dr["CreateDate"] = Comm.RemoveSpecialSymbols(dr["CreateDate"].ToString());
                dr["StartTime"] = Comm.RemoveSpecialSymbols(dr["StartTime"].ToString());
                dr["CompleteTime"] = Comm.RemoveSpecialSymbols(dr["CompleteTime"].ToString());
                dr["WorkItem"] = Comm.RemoveSpecialSymbols(dr["WorkItem"].ToString());
                dr["MasterDepartment"] = Comm.RemoveSpecialSymbols(dr["MasterDepartment"].ToString());
                dr["MasterPeople"] = Comm.RemoveSpecialSymbols(dr["MasterPeople"].ToString());
                dr["StrategicCategory"] = Comm.RemoveSpecialSymbols(dr["StrategicCategory"].ToString());

                dt.Rows.Add(dr);
            }

            //处理新行
            DataRow newdr = dt.NewRow();
            newdr["StrategicPlanId"] = "";
            newdr["StrategicPlanSortId"] = this.txtStrategicPlanSortId.Text;
            newdr["StrategicPlanYear"] = this.drpStrategicDate.SelectedItem.Text;
            newdr["CreateDate"] = DateTime.Now.ToString();
            newdr["StartTime"] = this.txtStartDay.Text;
            newdr["CompleteTime"] = this.txtEndDay.Text;
            newdr["WorkItem"] = this.txtPlanContent.Text;
            newdr["MasterDepartment"] = this.drpDepartment.SelectedItem.Text;
            newdr["MasterPeople"] = this.txtMasterPeople.Value;
            newdr["StrategicCategory"] = this.drpStrategyCategory.SelectedItem.Text;
            dt.Rows.Add(newdr);
            DataView dv = new DataView(dt);
            dv.Sort = "StrategicPlanSortId";
            this.gdvStrategicPlan.DataSource = dv;
            this.gdvStrategicPlan.DataBind();
            this.txtStrategicPlanSortId.Text = Convert.ToString(this.gdvStrategicPlan.Rows.Count + 1);
        }
        else
        {
            Utility.HttpHelp.alert("添加失败，请联系管理员添加战略计划分类！", Page);
        }
    }


    /// <summary>
    /// 清空输入项
    /// </summary>
    void clear()
    {

        this.txtEndDay.Text = "";
        this.txtMasterPeople.Value = "";
        this.txtPlanContent.Text = "";
        this.txtStartDay.Text = "";
        this.txtStrategicPlanSortId.Text = "";
    }


    /// <summary>
    /// 保存计划到数据库
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        for (int i = 0; i < this.gdvStrategicPlan.Rows.Count; i++)
        {
            CheckBox _ckb = (CheckBox)this.gdvStrategicPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            //判断将记录前打勾并且id为空的添加到集合
            if (_ckb.Checked == true && this.gdvStrategicPlan.DataKeys[i]["StrategicPlanId"].ToString().Length == 0)
            {
                _count++;
                object[] _value = new object[9];
                //计划序号
                _value[0] = this.gdvStrategicPlan.Rows[i].Cells[1].Text;
                //创建时间
                _value[1] = DateTime.Now;
                //开始时间
                _value[2] = Convert.ToDateTime(this.gdvStrategicPlan.Rows[i].Cells[3].Text);
                //结束时间
                _value[3] = Convert.ToDateTime(this.gdvStrategicPlan.Rows[i].Cells[4].Text);
                //计划内容
                _value[4] = this.gdvStrategicPlan.Rows[i].Cells[2].Text;
                //部门
                _value[5] = this.gdvStrategicPlan.Rows[i].Cells[5].Text;
                //责任人
                _value[6] = this.gdvStrategicPlan.Rows[i].Cells[6].Text;
                //战略计划类别
                _value[7] = this.gdvStrategicPlan.Rows[i].Cells[7].Text;
                //计划年份
                _value[8] = this.drpStrategicDate.SelectedItem.Text;
                _valueList.Add(_value);
            }
        }
        if (_valueList.Count > 0)
        {
            StrategicPlanMgr dllStrategicPlanMgr = new StrategicPlanMgr();
            int result = dllStrategicPlanMgr.addWithTransaction(_valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加战略计划未成功，请重试！");
            }
        }
        if (_count > 0 && _valueList.Count == 0)
        {
            Utility.HttpHelp.alert("选择的战略计划已保存！", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择要保存的战略计划！", Page);
            return;
        }
        bindGridView();
        this.txtStrategicPlanSortId.Text = Convert.ToString(this.gdvStrategicPlan.Rows.Count + 1);
        this.ckbAllSelect.Checked = false;

    }



    /// <summary>
    /// 全选事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ckbAllSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (this.ckbAllSelect.Checked == true)
        {
            for (int i = 0; i < this.gdvStrategicPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvStrategicPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < this.gdvStrategicPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvStrategicPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = false;
            }
        }
    }




    /// <summary>
    /// GridView绑定行事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvStrategicPlan_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            Button btn = (Button)e.Row.Cells[8].FindControl("btnDel");
            if (objUserInfo.UserName == "admin")
            {
                btn.Enabled = true;
            }
            else
            {
                btn.Enabled = false;
            }
            //处理日期格式
            e.Row.Cells[3].Text = Convert.ToDateTime(e.Row.Cells[3].Text).ToShortDateString();
            e.Row.Cells[4].Text = Convert.ToDateTime(e.Row.Cells[4].Text).ToShortDateString();
        }
    }




    /// <summary>
    /// 生成DataTable
    /// </summary>
    /// <returns>DataTable</returns>
    DataTable genDataTable()
    {
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn("StrategicPlanId");
        dt.Columns.Add(dc);
        dc = new DataColumn("StrategicPlanSortId");
        dt.Columns.Add(dc);
        dc = new DataColumn("StrategicPlanYear");
        dt.Columns.Add(dc);
        dc = new DataColumn("CreateDate");
        dt.Columns.Add(dc);
        dc = new DataColumn("StartTime");
        dt.Columns.Add(dc);
        dc = new DataColumn("CompleteTime");
        dt.Columns.Add(dc);
        dc = new DataColumn("WorkItem");
        dt.Columns.Add(dc);
        dc = new DataColumn("MasterDepartment");
        dt.Columns.Add(dc);
        dc = new DataColumn("MasterPeople");
        dt.Columns.Add(dc);
        dc = new DataColumn("StrategicCategory");
        dt.Columns.Add(dc);
        return dt;
    }

    /// <summary>
    /// 将GridView内容导入DataTable
    /// </summary>
    /// <returns></returns>
    DataTable GridViewToDataTable(GridView _gdv, DataTable _dt)
    {
        //获取当前GridView内容导入DataTable
        for (int i = 0; i < _gdv.Rows.Count; i++)
        {
            DataRow dr = _dt.NewRow();
            //战略计划id
            dr["StrategicPlanId"] = _gdv.DataKeys[i]["StrategicPlanId"].ToString();
            //战略计划排序id
            dr["StrategicPlanSortId"] = _gdv.Rows[i].Cells[1].Text;
            //战略计划年份
            dr["StrategicPlanYear"] = this.drpStrategicDate.SelectedItem.Text;
            //战略计划创建时间
            dr["CreateDate"] = DateTime.Now;
            //战略计划开始时间
            dr["StartTime"] = Convert.ToDateTime(_gdv.Rows[i].Cells[3].Text);
            //完成时间
            dr["CompleteTime"] = Convert.ToDateTime(_gdv.Rows[i].Cells[4].Text);
            //计划内容
            dr["WorkItem"] = _gdv.Rows[i].Cells[2].Text;
            //部门
            dr["MasterDepartment"] = _gdv.Rows[i].Cells[5].Text;
            //负责人
            dr["MasterPeople"] = _gdv.Rows[i].Cells[6].Text;
            //战略类别
            dr["StrategicCategory"] = _gdv.Rows[i].Cells[7].Text;
            _dt.Rows.Add(dr);
        }
        return _dt;
    }

    /// <summary>
    /// 删除事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        Model.UserInfo objUserInfo = new Model.UserInfo();
        if (objUserInfo.UserName != "admin")
        {
            Utility.Error.RedirectErrorPage("只有Admin账户可以删除！");
            return;
        }
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引

        DataTable dt = genDataTable();
        //获取已有的记录
        dt = GridViewToDataTable(this.gdvStrategicPlan, dt);

        //删除DataTable中的记录
        dt.Rows[_rowIndex].Delete();

        if (this.gdvStrategicPlan.DataKeys[_rowIndex]["StrategicPlanId"].ToString().Length > 0)
        {
            //获得记录id
            int StrategicPlanId = Convert.ToInt32(this.gdvStrategicPlan.DataKeys[_rowIndex]["StrategicPlanId"].ToString());
            object[] _value = new object[1];
            _value[0] = StrategicPlanId;
            StrategicPlanMgr dllStrategicPlanMgr = new StrategicPlanMgr();
            int result = dllStrategicPlanMgr.del(StrategicPlanMgr.field.StrategicPlanId.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("删除战略计划未成功，请重试！");
            }
        }

        this.gdvStrategicPlan.DataSource = dt;
        this.gdvStrategicPlan.DataBind();
        this.txtStrategicPlanSortId.Text = Convert.ToString(this.gdvStrategicPlan.Rows.Count + 1);
    }


    /// <summary>
    /// 年份改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGridView();
    }

    protected void btnImport_Click(object sender, EventArgs e)
    {
        //得到选择的战略周期值         
        string _url = "StrategicPlanImport.aspx?StrategicDate=" + this.drpStrategicDate.SelectedValue;
        Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=160,width=900,top=300,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
        return;

    }
}
